package com.sql;

import java.sql.*;
 
public class Conn {
	private static Connection con = null;
	private static final String DRIVER = "com.mysql.jdbc.Driver"; //定义驱动字符串
	private static final String URL = "jdbc:mysql://127.0.0.1:3306/bank?useUnicode=true&characterEncoding=UTF-8"; //定义URL
	private static final String NAME = "root"; //定义用户名
	private static final String PASSWORD="123456"; //定义密码
	static{ //加载一次即可得到驱动
		try{
			Class.forName(DRIVER); //加载驱动
		}catch (ClassNotFoundException e){
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection(){ //得到数据库连接
		try{
			con=DriverManager.getConnection(URL,NAME,PASSWORD); //创建连接
		}catch(SQLException e){
			e.printStackTrace();
		}return con;
	}
	
	public static void closeCon(Connection con){ //关闭Connection
		try{
			if(con!=null){
				con.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public static void closePt(PreparedStatement pt){ //关闭PreparedStatement
		try{
			if(pt!=null){
				pt.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void closeRs(ResultSet rs){//关闭ResultSet
		try{
			if(rs!=null){
				rs.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

